const Student = require("../models/student");
const Grade = require("../models/Grade");
Student.belongsTo(Grade)
// const validate = require("../utils/extendValidate");
const validate = require("validate.js")
const moment = require("moment")


// 对规则的延申
validate.validators.validateClass= async function(value){
    // 从数据库中看一下当前的班级是否存在
    const isexist = await Grade.findByPk(value)
    if(isexist){
        return 
    }else{
        return "is not exist"
    }
}
// validate.validators.format.message="你错了 嘻嘻嘻嘻"
// 定义验证规则
const rules = {
    stuName: {
        presence: {
            allowEmpty: false
        },
        type: 'string',
        length: {
            minimum: 2,
            maximum: 10
        }
    },
    birthDay: {
        presence: {
            allowEmpty: false
        },
        datetime: {
            dateOnly: true,
            earliest:moment.utc().subtract(20,'y'),
            lastest:moment.utc().subtract(5,'y')

        },
        
        
    },
    sex:{
        presence:true,
        type:'boolean'
    },
    phone:{
        presence:true,
        format:/^1\d{10}/
    },
    GradeId:{
        presence:true,
        numericality:{
            onlyInteger:true,
            strict:false
        },
        validateClass:true
    }
}

exports.addStudent = async function (stuObj) {
    
       await validate.async(stuObj, rules)
        // 通过了 各种验证就进行添加
        // 出错就报错
        const ins = await Student.create(stuObj);
         return ins.toJSON();
    
   
}

exports.delStudent = async function (id) {
    const ins = await Student.destroy({
        where: {
            id
        }
    })
    return ins
}

exports.updateStudent = async function (id, updateObj) {
    const ins = await Student.update(updateObj, {
        where: {
            id
        }
    })

    return ins
}

// 根据id得到某个学生
exports.getStudentById = async function (id) {
    const resp = await Student.findByPk(id);
    if (resp) {
        // 找到了
        return JSON.parse(JSON.stringify(resp))
    } else {
        return null
    }
}
// 根据id查询学生信息 并且得到班级信息
exports.getStudentInfo = async function (id) {
    const condition = {
        id
    }
    const resp = await Student.findOne({
        where: condition,
        include: [Grade]
    })
    if (resp) {
        return JSON.parse(JSON.stringify(resp))
    } else {
        return null
    }
}

// 根据分页得到学生
exports.getStudents = async function (page = 1, size = 10, sex = -1, GradeId = -1) {

    let condition = {};
    if (sex !== -1) {
        //传递了
        condition.sex = sex
    }
    if (GradeId !== -1) {
        condition.GradeId = GradeId
    }
    const resp = await Student.findAll({
        attributes: ["id", "stuName", "birthDay"],
        where: condition,
        include: [Grade],
        offset: (page - 1) * size,
        limit: +size
    })
    const count = await Student.count();
    return {
        datas: JSON.parse(JSON.stringify(resp)),
        total: count
    }
}

// 另外一种查询并返回长度的方法
exports.getStudentsWithTotal = async function (page = 1, size = 10, sex = -1, GradeId = -1) {
    let condition = {};
    if (sex !== -1) {
        //传递了
        condition.sex = sex
    }
    if (GradeId !== -1) {
        condition.GradeId = GradeId
    }
    const {
        count,
        rows
    } = await Student.findAndCountAll({
        attributes: ["id", "stuName", "birthDay"],
        where: condition,
        include: [Grade],
        offset: (page - 1) * size,
        limit: +size
    })

    return {
        count,
        datas: JSON.parse(JSON.stringify(rows))
    }
}